package com.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.system.mapper.BuyerMapper;
import com.system.pojo.SalePlanDto;
import com.system.pojo.TS_ITEM_DOC;
import com.system.pojo.buyerDoC;
import com.system.service.BuyerService;
import com.system.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BuyerController {
    @Autowired
    private BuyerMapper bDao;

    @Autowired
    private BuyerService buyerService;

    @RequestMapping("deletebuyer")
    @ResponseBody
    public String deletebuyer(@RequestBody String s) {
        JSONObject object = new JSONObject();
        try {
            System.out.println("输入 : " + s);//接受前端参数
            s = s.replace("[", "");
            s = s.replace("]", "");
            JSONObject jsonobject = JSONObject.parseObject(s);
            System.out.println(jsonobject);
            String aDDRESS = jsonobject.getString("aDDRESS");
            System.out.println(aDDRESS);
            bDao.deleteBuyer(aDDRESS);

            object.put("code", 0);
            object.put("msg", "");
            return object.toJSONString();

        } catch (Exception e) {
            e.printStackTrace();
            return object.toJSONString();
        }
    }


    @RequestMapping("getSupplier")
    @ResponseBody
    public String getSupplier(Integer page, Integer limit) {
        String json = null;
        int pl = page * limit - limit;
        List<buyerDoC> list = bDao.selectbuyerDOC();
        List<buyerDoC> list2 = bDao.selectListLimt2(pl, limit);
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", list.size());
        jsonObject.put("data", list2);
        return jsonObject.toJSONString();

    }

    //显示
    @RequestMapping("selectPingZheng")
    @ResponseBody
    public String selectPingZheng(Integer page, Integer limit) {
        String json = null;
        int pl = page * limit - limit;
        JSONObject object = new JSONObject();
        try {
            System.out.println("执行查询语句");
            //查询数据库

            System.out.println(pl);
            List<buyerDoC> list1 = bDao.selectbuyerDOC();
            List<buyerDoC> list = bDao.selectListLimt2(pl, limit);


           /* System.out.println(list1);
            String biaoti = null;*/
            /*
             * for(Formmain0064 formmain0064 : list1) {
             * System.out.println("状态 : "+formmain0064.getField0012()); String a =
             * wdMapper.selectDanjuType(formmain0064.getField0012()); if(a == null ||
             * "".equals(a)) { formmain0064.setField0012("否"); }else {
             * formmain0064.setField0012(a); } }
             */
            object.put("code", 0);
            object.put("msg", "");
            object.put("count", list1.size());
            object.put("data", list);
            json = object.toJSONString();
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return json;
        }
    }


    //存货列表
    @GetMapping(value = "/getItemList")
    @ResponseBody
    public Object getItemList() {
        List<TS_ITEM_DOC> itemList = buyerService.getItemList();
        return JSON.toJSONString(new JsonResult(itemList));
    }

    //查询商户列表
    @GetMapping(value = "/getVenderList")
    @ResponseBody
    public Object getVenderList() {
        List<buyerDoC> itemList = buyerService.getVenderList();
        return JSON.toJSONString(new JsonResult(itemList));

    }

    //新增售卖计划
    @PostMapping("/insertks")
    @ResponseBody
    public Object insertks(@RequestBody SalePlanDto salePlanDto) {
        System.out.println(salePlanDto);
        buyerService.addSalePlan(salePlanDto);
        return JSON.toJSONString(new JsonResult());
    }
}
